package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.mapper.*;
import com.smile.model.domain.*;
import com.smile.model.dto.request.*;
import com.smile.model.response.response.*;
import com.smile.service.ModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ModelServiceImpl implements ModelService {


    @Autowired
    private BrandMapper brandMapper;


    @Autowired
    private DeviceSeriesMapper deviceSeriesMapper;

    @Autowired
    private DeviceModelMapper deviceModelMapper;

    @Override
    public List<GetBrandListResponseDTO> getBrandList(){
        QueryWrapper<FeisuanBrand> queryWrapper = new QueryWrapper<>();
        List<FeisuanBrand> feisuanBrand = brandMapper.selectList(queryWrapper);
        List<GetBrandListResponseDTO> getBrandList = BeanUtil.copyToList(feisuanBrand, GetBrandListResponseDTO.class);
        return getBrandList;
    }

    @Override
    public List<GetSeriesListResponseDTO> getModelList(Long brandId){
        QueryWrapper<FeisuanDeviceSeries> queryWrapper = new QueryWrapper<>();
        List<FeisuanDeviceSeries> feisuanDeviceSeries = deviceSeriesMapper.selectList(queryWrapper);
        System.out.println(feisuanDeviceSeries);
        List<GetSeriesListResponseDTO> seriesList = new ArrayList<>();
        for(FeisuanDeviceSeries deviceSeries : feisuanDeviceSeries){
            GetSeriesListResponseDTO getSeriesListResponseDTO = new GetSeriesListResponseDTO();
            getSeriesListResponseDTO.setSeriesName(deviceSeries.getSeriesName());
            getSeriesListResponseDTO.setId(deviceSeries.getId());
            QueryWrapper<FeisuanDeviceModel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("series_id",deviceSeries.getId());
            List<FeisuanDeviceModel> list = deviceModelMapper.selectList(queryWrapper1);
            List<GetModelListResponseDTO> getModelList = BeanUtil.copyToList(list, GetModelListResponseDTO.class);
            getSeriesListResponseDTO.setModels(getModelList);
            seriesList.add(getSeriesListResponseDTO);
        }
        return seriesList;
    }

    @Override
    public List<FeisuanDeviceModelResponseDTO> getModelList2(Map map){
        QueryWrapper<FeisuanDeviceModel> queryWrapper = new QueryWrapper<>();
        if(map.containsKey("modelName")){
            queryWrapper.like("model_name",map.get("modelName"));
        }

        if(map.containsKey("deviceModelKey")){
            queryWrapper.like("device_model_key",map.get("deviceModelKey"));
        }
        queryWrapper.eq("domain","0");
        List<FeisuanDeviceModel> feisuanDeviceSeries = deviceModelMapper.selectList(queryWrapper);
        List<FeisuanDeviceModelResponseDTO> modelList = BeanUtil.copyToList(feisuanDeviceSeries, FeisuanDeviceModelResponseDTO.class);
        return modelList;
    }


}
